package com.ruoyi.web.controller.snmp;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.snmp.ip.Ping;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SnmpGroupdevice;
import com.ruoyi.system.service.ISnmpGroupdeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.*;
import java.util.stream.Collectors;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2025-02-05
 */
@RestController
@RequestMapping("/snmp/groupdevice")
public class SnmpGroupdeviceController extends BaseController
{
    @Autowired
    private ISnmpGroupdeviceService snmpGroupdeviceService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('snmp:groupdevice:list')")
    @GetMapping("/list")
    public TableDataInfo list(SnmpGroupdevice snmpGroupdevice)
    {
        startPage();
        List<SnmpGroupdevice> list = snmpGroupdeviceService.selectSnmpGroupdeviceList(snmpGroupdevice);
        return getDataTable(list);
    }
    @PreAuthorize("@ss.hasPermi('snmp:groupdevice:list')")
    @GetMapping("/alldevice")
    public TableDataInfo alllist(SnmpGroupdevice snmpGroupdevice) throws Exception {

        startPage();
        List<SnmpGroupdevice> arryObj;
        arryObj = snmpGroupdeviceService.getAllGroupDevice(snmpGroupdevice);
        // 创建一个固定大小的线程池
        int THREAD_POOL_SIZE = 10; // 根据系统资源调整线程池大小
        ExecutorService executorService = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
        // 为每个IP创建一个Callable任务
        List<Future<Boolean>> futures = arryObj.stream()
                .map(itme -> executorService.submit(() -> Ping.GetSnmpSuccess(itme.getGroupip())))
                .collect(Collectors.toList());
        for (int i = 0; i < futures.size(); i++) {
            try {
                Boolean isOnline = futures.get(i).get();
                arryObj.get(i).setOnlineStatus(isOnline ? "在线" : "无法访问");
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
                arryObj.get(i).setOnlineStatus("未知状态");
            }
        }

        // 关闭线程池
        executorService.shutdown();
        try {
            if (!executorService.awaitTermination(60, TimeUnit.SECONDS)) {
                executorService.shutdownNow(); // 强制关闭线程池
            }
        } catch (InterruptedException e) {
            executorService.shutdownNow(); // 强制关闭线程池
            e.printStackTrace();
        }


        //List<SnmpGroupdevice> list = snmpGroupdeviceService.selectSnmpGroupdeviceList(snmpGroupdevice);
        return getDataTable(arryObj);
    }


    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('snmp:groupdevice:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SnmpGroupdevice snmpGroupdevice)
    {
        List<SnmpGroupdevice> list = snmpGroupdeviceService.selectSnmpGroupdeviceList(snmpGroupdevice);
        ExcelUtil<SnmpGroupdevice> util = new ExcelUtil<SnmpGroupdevice>(SnmpGroupdevice.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('snmp:groupdevice:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(snmpGroupdeviceService.selectSnmpGroupdeviceById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('snmp:groupdevice:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SnmpGroupdevice snmpGroupdevice)
    {
        return toAjax(snmpGroupdeviceService.insertSnmpGroupdevice(snmpGroupdevice));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('snmp:groupdevice:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SnmpGroupdevice snmpGroupdevice)
    {
        return toAjax(snmpGroupdeviceService.updateSnmpGroupdevice(snmpGroupdevice));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('snmp:groupdevice:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(snmpGroupdeviceService.deleteSnmpGroupdeviceByIds(ids));
    }
}
